package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.login.Userparam;
import com.example.demo.datasource.multi.dao.*;
import com.example.demo.entity.individualsubmit.individualsubmit;
import com.example.demo.entity.individualsubmit.tablevalues;
import com.example.demo.entity.organizationsubmit.organizationsubmit;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class applicationcontroller {

@Autowired
com.example.demo.entity.individualsubmit.individualsubmit individualsubmit;
@Autowired
com.example.demo.datasource.multi.dao.insertmapper insertmapper;
    @Autowired
    com.example.demo.datasource.multi.dao.accountinfomapper accountinfomapper;
    @Autowired
    com.example.demo.entity.organizationsubmit.organizationsubmit organizationsubmit;
    @Autowired
    com.example.demo.datasource.multi.dao.selectstringmapper selectstringmapper;
    @Autowired
    com.example.demo.datasource.multi.dao.updatemapper updatemapper;
    @Autowired
    Deletemapper deletemapper;

    @ResponseBody
    @RequestMapping("/individualsubmit")
    public JSON individualsubmit(@RequestBody individualsubmit params, HttpServletRequest request){
        System.out.println(params);

        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());

        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String createtime=simpleDateFormat1.format(new Date());

        String type = "individual";

        int nownumber = Integer.parseInt(params.getWfnum())+1;

        if (params.getTtype().equals("edit")){

            String sql5 = "update application a set " +
                    "a.studentname = '"+params.getStudentname()+"'," +
                    "a.studentid = '"+params.getStudentid()+"'," +
                    "a.contact = '"+params.getContact()+"'," +
                    "a.sex = '"+params.getSex()+"'," +
                    "a.nation = '"+params.getNation()+"'," +
                    "a.politics = '"+params.getPolitics()+"'," +
                    "a.college = '"+params.getCollege()+"'," +
                    "a.profession = '"+params.getProfession()+"'," +
                    "a.studentclass = '"+params.getStudentclass()+"'," +
                    "a.averagescores = '"+params.getAveragescores()+"'," +
                    "a.post = '"+params.getPost()+"'," +
                    "a.applicationawards = '"+params.getApplicationawards()+"'" +
                    "WHERE " +
                    "a.pkid = '"+params.getPpkid()+"'";

            System.out.println(sql5);
            int result6 = updatemapper.update(sql5);
            System.out.println(result6);



            String sql6 = "insert into workflow(pkid,number,previousnumber,`status`,type,HANDLER,handletime,handleaction,isread,dr) " +
                    "VALUES(" +
                    "'"+params.getPpkid()+"'," +
                    ""+nownumber+"," +
                    ""+params.getWfnum()+"," +
                    "'2'" +
                    ",'"+type+"'" +
                    ",'"+user.getUsername()+"'" +
                    ",'"+createtime+"'" +
                    ",'提交'" +
                    ",'true',0)";
            System.out.println(sql6);
            int result7= insertmapper.insert(sql6);
            System.out.println(result7);

            String sql7 = "DELETE from honors WHERE pkid = '"+params.getPpkid()+"'";
            int result9 = deletemapper.delete(sql7);
            System.out.println(result9);

            List<tablevalues> tv = params.getMylist();

            for (tablevalues ttv:tv
            ) {
                String sql1 = "insert into honors (pkid,name,no,time) VALUES(" +
                        "'"+params.getPpkid()+"'," +
                        "'"+ttv.getName()+"'," +
                        "'"+ttv.getNo()+"'," +
                        "'"+ttv.getTime()+"')";
                System.out.println(sql1);
                int result1= insertmapper.insert(sql1);
                System.out.println(result1);
            }


            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result","success");
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;

        }else {
            //时间格式化格式
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmssSSS");
            //获取当前时间并作为时间戳
            String pkid=simpleDateFormat.format(new Date());
            //时间格式化格式

            String approvalstatus = "2";
            List<tablevalues> tv = params.getMylist();

            for (tablevalues ttv:tv
            ) {
                String sql1 = "insert into honors (pkid,name,no,time) VALUES(" +
                        "'"+pkid+"'," +
                        "'"+ttv.getName()+"'," +
                        "'"+ttv.getNo()+"'," +
                        "'"+ttv.getTime()+"')";
                System.out.println(sql1);
                int result1= insertmapper.insert(sql1);
                System.out.println(result1);
            }

            String sql2="select id from user where username = '"+user.getUsername()+"'";
            int userid=accountinfomapper.getuserid(sql2);


            String sql3 = "insert into application (userid,studentname,studentid,contact,sex,nation,politics,college,profession,studentclass,averagescores,post,applicationawards,approvalstatus,createtime,type,pkid,workflownumber,numberofedits" +
                    ") VALUES(" +
                    ""+userid+"," +
                    "'"+params.getStudentname()+"'," +
                    "'"+params.getStudentid()+"'," +
                    "'"+params.getContact()+"'," +
                    "'"+params.getSex()+"'," +
                    "'"+params.getNation()+"'," +
                    "'"+params.getPolitics()+"'," +
                    "'"+params.getCollege()+"'," +
                    "'"+params.getProfession()+"'," +
                    "'"+params.getStudentclass()+"'," +
                    "'"+params.getAveragescores()+"'," +
                    "'"+params.getPost()+"'," +
                    "'"+params.getApplicationawards()+"'," +
                    "'"+approvalstatus+"'," +
                    "'"+createtime+"'," +
                    "'"+type+"'," +
                    "'"+pkid+"'," +
                    "'2'," +
                    "1" +
                    ")";
            System.out.println(sql3);
            int result3= insertmapper.insert(sql3);
            System.out.println(result3);

            String sql5 = "insert into workflow(pkid,number,previousnumber,`status`,type,HANDLER,handletime,handleaction,isread,dr) " +
                    "VALUES(" +
                    "'"+pkid+"'," +
                    "1," +
                    "0," +
                    "'1'" +
                    ",'"+type+"'" +
                    ",'"+user.getUsername()+"'" +
                    ",'"+createtime+"'" +
                    ",'编辑'" +
                    ",'true',0)";
            System.out.println(sql5);
            int result6= insertmapper.insert(sql5);
            System.out.println(result6);

            String sql4 = "insert into workflow(pkid,number,previousnumber,`status`,type,HANDLER,handletime,handleaction,isread,dr) " +
                    "VALUES(" +
                    "'"+pkid+"'," +
                    "2," +
                    "1," +
                    "'2'" +
                    ",'"+type+"'" +
                    ",'"+user.getUsername()+"'" +
                    ",'"+createtime+"'" +
                    ",'提交'" +
                    ",'true',0)";
            System.out.println(sql4);
            int result4= insertmapper.insert(sql4);
            System.out.println(result4);


            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result","success");
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }

    }

    @ResponseBody
    @RequestMapping("/organizationsubmit")
    public JSON organizationsubmit(@RequestBody organizationsubmit params, HttpServletRequest request){
        System.out.println(params);
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        String type = "organization";
        //时间格式化格式
        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String createtime=simpleDateFormat1.format(new Date());



        if (params.getTtype().equals("edit")){
            int nownumber = Integer.parseInt(params.getWfnum())+1;
            String sql5 = "update application a set " +
                    "a.college = '"+params.getCollege()+"'," +
                    "a.profession = '"+params.getProfession()+"'," +
                    "a.studentclass = '"+params.getStudentclass()+"'," +
                    "a.studentnumber = '"+params.getStudentnumber()+"'," +
                    "a.director = '"+params.getDirector()+"'," +
                    "a.applicationawards = '"+params.getApplicationawards()+"'," +
                    "a.evaluate = '"+params.getEditvalues()+"' " +
                    "WHERE " +
                    "a.pkid = '"+params.getPpkid()+"'";

            System.out.println(sql5);
            int result6 = updatemapper.update(sql5);
            System.out.println(result6);



            String sql6 = "insert into workflow(pkid,number,previousnumber,`status`,type,HANDLER,handletime,handleaction,isread,dr) " +
                    "VALUES(" +
                    "'"+params.getPpkid()+"'," +
                    ""+nownumber+"," +
                    ""+params.getWfnum()+"," +
                    "'2'" +
                    ",'"+type+"'" +
                    ",'"+user.getUsername()+"'" +
                    ",'"+createtime+"'" +
                    ",'提交'" +
                    ",'true',0)";
            System.out.println(sql6);
            int result7= insertmapper.insert(sql6);
            System.out.println(result7);

            String sql7 = "DELETE from honors WHERE pkid = '"+params.getPpkid()+"'";
            int result9 = deletemapper.delete(sql7);
            System.out.println(result9);

            List<tablevalues> tv = params.getMylist();

            for (tablevalues ttv:tv
            ) {
                String sql1 = "insert into honors (pkid,name,no,time) VALUES(" +
                        "'"+params.getPpkid()+"'," +
                        "'"+ttv.getName()+"'," +
                        "'"+ttv.getNo()+"'," +
                        "'"+ttv.getTime()+"')";
                System.out.println(sql1);
                int result1= insertmapper.insert(sql1);
                System.out.println(result1);
            }

            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result","success");
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;

        }else {
            //时间格式化格式
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmssSSS");
            //获取当前时间并作为时间戳
            String pkid=simpleDateFormat.format(new Date());


            String approvalstatus = "2";
            List<tablevalues> tv = params.getMylist();

            for (tablevalues ttv:tv
            ) {
                String sql1 = "insert into honors (pkid,name,no,time) VALUES(" +
                        "'"+pkid+"'," +
                        "'"+ttv.getName()+"'," +
                        "'"+ttv.getNo()+"'," +
                        "'"+ttv.getTime()+"')";
                System.out.println(sql1);
                int result1= insertmapper.insert(sql1);
                System.out.println(result1);
            }


            String sql2="select id from user where username = '"+user.getUsername()+"'";
            int userid=accountinfomapper.getuserid(sql2);


            String sql3 = "insert into application (userid,college,profession,studentclass,director,evaluate,studentnumber,applicationawards,approvalstatus,createtime,type,pkid,workflownumber,numberofedits" +
                    ") VALUES(" +
                    ""+userid+"," +
                    "'"+params.getCollege()+"'," +
                    "'"+params.getProfession()+"'," +
                    "'"+params.getStudentclass()+"'," +
                    "'"+params.getDirector()+"'," +
                    "'"+params.getEditvalues()+"'," +
                    "'"+params.getStudentnumber()+"'," +
                    "'"+params.getApplicationawards()+"'," +
                    "'"+approvalstatus+"'," +
                    "'"+createtime+"'," +
                    "'"+type+"'," +
                    "'"+pkid+"'," +
                    "'2'," +
                    "1" +
                    ")";
            System.out.println(sql3);
            int result3= insertmapper.insert(sql3);
            System.out.println(result3);

            String sql5 = "insert into workflow(pkid,number,previousnumber,`status`,type,HANDLER,handletime,handleaction,isread,dr) " +
                    "VALUES(" +
                    "'"+pkid+"'," +
                    "1," +
                    "0," +
                    "'1'" +
                    ",'"+type+"'" +
                    ",'"+user.getUsername()+"'" +
                    ",'"+createtime+"'" +
                    ",'编辑'" +
                    ",'true',0)";
            System.out.println(sql5);
            int result6= insertmapper.insert(sql5);
            System.out.println(result6);

            String sql4 = "insert into workflow(pkid,number,previousnumber,`status`,type,HANDLER,handletime,handleaction,isread,dr) " +
                    "VALUES(" +
                    "'"+pkid+"'," +
                    "2," +
                    "1," +
                    "'2'" +
                    ",'"+type+"'" +
                    ",'"+user.getUsername()+"'" +
                    ",'"+createtime+"'" +
                    ",'提交'" +
                    ",'true',0)";
            System.out.println(sql4);
            int result4= insertmapper.insert(sql4);
            System.out.println(result4);




            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result","success");
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;

        }


    }

}
